package com.es.commons.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 描述:
 * Old-AgeCareSystem-
 *
 * @author Derek
 * @create 2021-12-09 11:58
 */
@Configuration
public class OrderRabbitConfigurer {

    @Bean
    public Queue orderStatusQueue(){
        Map<String,Object> args = new HashMap<>();
        //只存活 60秒
        args.put("x-message-ttl", 5000);
        //死了找交互机
        args.put("x-dead-letter-exchange", "order-exchange-dlx");
        args.put("x-dead-letter-routing-key","order-dlx-key");
        //路由到指定的队列
        Queue queue = new Queue("order-status-queue",true,false,false,args);
        return queue;
    }
    /**
     * 死信交换机
     * @return
     */
    @Bean
    public DirectExchange orderStatusExchange(){
        DirectExchange exchange = new DirectExchange("order-status-exchange");
        return exchange;
    }

    @Bean
    public Binding orderStatusQueueExchangeBinding(){
        return BindingBuilder.bind(orderStatusQueue()).to(orderStatusExchange()).with("order-status-key");
    }


    /**
     * @return 死信队列
     * 超时了的信息会发到这里来
     */
    @Bean
    public Queue orderQueueDLX(){
        Queue queue = new Queue("order-queue-dlx");
        return  queue;
    }

    /**
     * 死信交换机
     * @return
     */
    @Bean
    public DirectExchange orderExchangeDLX(){
        DirectExchange exchange = new DirectExchange("order-exchange-dlx");
        return exchange;
    }

    /**死信队列交换机绑定
     * @return
     */
    @Bean
    public Binding orderQueueExchangeBinding(){
        return BindingBuilder.bind(orderQueueDLX()).to(orderExchangeDLX()).with("order-dlx-key");
    }
}
